package com.sam.im.heiqun.uis.activities;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.CircleNums;
import com.sam.im.heiqun.uis.adapters.CircleDetailAdapter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.sam.im.heiqun.view.PullToRefreshRecyclerView;
import com.sam.im.heiqun.widgets.imagepicker.MediaBean;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import com.sam.im.heiqun.utils.StringUtils;

public class CircleDetailActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.recyclerview)
    PullToRefreshRecyclerView recyclerview;
    @BindView(R.id.ok)
    TextView ok;
    private CircleDetailAdapter adapter;
    private List<CircleNums> datas = new ArrayList<>();
    private List<CircleNums> circleNumses =new ArrayList<>();

    @Override
    public int getContentViewId() {
        return R.layout.activity_circle_detail;
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.message);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        adapter = new CircleDetailAdapter(this, datas);
        recyclerview.setAdapter(adapter);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setPullRefreshEnabled(false);
        recyclerview.setLoadingMoreEnabled(false);
        ok.setVisibility(View.VISIBLE);
        ok.setEnabled(true);
//        ok.setBackgroundResource(R.drawable.shape_sure_bt);
        ok.setText(getResources().getString(R.string.history));
        adapter.setOnlongclikcklistenler(new CircleDetailAdapter.Onlongclikcklistenler() {
            @Override
            public void onlongclick(int position) {
                dialog(datas.get(position).getId(),position);
            }
        });
        circleNumses = CircleNums.find(CircleNums.class, "unicid=? and isread=? and type!=?", new String[]{ToolsUtils.getMyUserId(), "0", Constant.MESSAGE_TYPR_CIRCLE+""});
        if (circleNumses != null && circleNumses.size() != 0) {
            datas.addAll(circleNumses);
            adapter.notifyDataSetChanged();
            EventBus.getDefault().post("动态已读");
        } else {

        }
        String mycircle = getIntent().getStringExtra("key");
        if (mycircle != null && !StringUtils.isEmpty(mycircle)) {
            ok.setVisibility(View.GONE);
            ok.setEnabled(false);
            List<CircleNums> circleNumses1 = CircleNums.find(CircleNums.class, "unicid=? and isread=? and type!=?", new String[]{ToolsUtils.getMyUserId(), "1", Constant.MESSAGE_TYPR_CIRCLE+""});
            if (circleNumses1 != null && circleNumses1.size() > 0) {
                for (int j = 0; j <circleNumses1.size(); j++) {
                    for (int g = 0; g < circleNumses.size(); g++) {
                       if (circleNumses1.get(j).getId()== circleNumses.get(g).getId()){
                           circleNumses1.remove(j);
                       }
                    }
                }
                //对别人点赞跟评论的消息进行排序，i--
                List<CircleNums> newList1=new ArrayList();
                if(circleNumses1!=null){
                    for (int i = circleNumses1.size()-1; i >0 ; i--) {
                        newList1.add(circleNumses1.get(i));
                    }
                }
                datas.addAll(newList1);
                adapter.notifyDataSetChanged();
            }
        }
    }



    @OnClick({R.id.pre_v_back, R.id.ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back:

                finish();
                break;
            case R.id.ok:
                ok.setVisibility(View.GONE);
                ok.setEnabled(false);
                List<CircleNums> circleNumses1 = CircleNums.find(CircleNums.class, "unicid=? and isread=?", new String[]{ToolsUtils.getMyUserId(), "1"});
                if (circleNumses1 != null && circleNumses1.size() > 0) {
                    datas.addAll(circleNumses1);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }
    protected void dialog(final long id, final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(CircleDetailActivity.this);
        builder.setMessage(getResources().getString(R.string.delete_ok));
        builder.setTitle(getResources().getString(R.string.tip));
        builder.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                CircleNums byId = CircleNums.findById(CircleNums.class, id);
                if (byId != null) {
                    byId.delete();
                    datas.remove(position);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    @Override
    protected void onDestroy() {
        for (int i = 0; i < circleNumses.size(); i++) {
            CircleNums circleNums = circleNumses.get(i);
            circleNums.setIsread("1");
            circleNums.save();
        }
        datas.clear();
        circleNumses.clear();
        super.onDestroy();
    }
}
